package com.zzedu.aop;

import com.zzedu.common.enums.CommonEnums;
import com.zzedu.common.exception.ServiceException;
import com.zzedu.common.utils.SecurityUtils;
import com.zzedu.common.utils.security.LoginUser;
import com.zzedu.domain.ZzSchool;
import com.zzedu.domain.ZzStudent;
import com.zzedu.domain.ZzTeacher;
import com.zzedu.enmuns.SchoolStateEnums;
import com.zzedu.service.IZzSchoolService;
import com.zzedu.service.IZzStudentService;
import com.zzedu.service.IZzTeacherService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 校区状态验证切面
 */
@Aspect
@Component
public class SchoolValidAspect {

    Logger logger = LoggerFactory.getLogger(SchoolValidAspect.class);
    @Autowired
    IZzSchoolService zzSchoolService;

    @Autowired
    IZzStudentService zzStudentService;

    @Autowired
    IZzTeacherService zzTeacherService;

    @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)||" +
            "@annotation(org.springframework.web.bind.annotation.RequestMapping)||" +
            "@annotation(org.springframework.web.bind.annotation.PostMapping)||" +
            "@annotation(org.springframework.web.bind.annotation.DeleteMapping)||" +
            "@annotation(org.springframework.web.bind.annotation.PutMapping)")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (null != null) {
            if (SecurityUtils.isStu()) {
                ZzStudent zzStudent = zzStudentService.getById(loginUser.getUserId());
                if (CommonEnums.YES != CommonEnums.getCode(zzStudent.getState())) {
                    logger.error("当前账号状态不正确,账号:{},id:{}", zzStudent.getTel(), zzStudent.getId());
                    throw new ServiceException("当前账号状态不正确");
                }
                ZzSchool zzSchool = this.zzSchoolService.getById(zzStudent.getSchoolId());
                if (null == zzSchool) {
                    logger.error("校区不存在,账号:{},id:{}", zzStudent.getTel(), zzStudent.getId());
                    throw new ServiceException("校区不存在");
                }
                SchoolStateEnums schoolStateEnums = SchoolStateEnums.getCode(zzSchool.getState());
                if (SchoolStateEnums.DISABLE == schoolStateEnums) {
                    logger.error("当前校区已停用,账号:{},id:{}", zzStudent.getTel(), zzStudent.getId());
                    throw new ServiceException("当前校区已停用");
                }
            } else {
                ZzTeacher teacher = zzTeacherService.getById(loginUser.getUserId());
                if (CommonEnums.YES != CommonEnums.getCode(teacher.getState())) {
                    logger.error("当前账号状态不正确,账号:{},id:{}", teacher.getTel(), teacher.getId());
                    throw new ServiceException("当前账号状态不正确");
                }
                ZzSchool zzSchool = this.zzSchoolService.getById(teacher.getSchoolId());
                if (null == zzSchool) {
                    logger.error("当前账号对应校区不存在,账号:{},id:{}", teacher.getTel(), teacher.getId());
                    throw new ServiceException("校区不存在");
                }
                SchoolStateEnums schoolStateEnums = SchoolStateEnums.getCode(zzSchool.getState());
                if (SchoolStateEnums.DISABLE == schoolStateEnums) {
                    logger.error("当前校区已停用,账号:{},id:{},学校:{},学校ID:{}", teacher.getTel(), teacher.getId(), zzSchool.getSchoolName(), zzSchool.getId());
                    throw new ServiceException("当前校区已停用");
                }
            }
        }
    }
}
